from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.views import View


class User():
    def __init__(self, id, name, age):
        self.id = id
        self.name = name
        self.age = age


# Create your views here.
def index(request):
    user = User(1, '张三', 18)
    user_data = {
        'id': user.id,
        'name': user.name,
        'age': user.age
    }
    # locals（） 返回本方法中的全部变量
    # 直接传user_data等价于传递{'id': user.id, 'name': user.name, 'age': user.age}
    return render(request,
                  'index.html',
                  locals())


def getUserById(request, id):
    user = User(id, '张三', 18)
    print(id)
    # return JsonResponse({
    #     'id': user.id,
    #     'name': user.name,
    #     'age': user.age
    # })
    return JsonResponse(user.__dict__)


def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    elif request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        # avatar = request.POST.get("avatar")
        avatar = request.FILES.get("avatar")
        hobby = request.POST.getlist("hobby")
        gender = request.POST.get("gender")
        print(username, password, avatar, hobby, gender)
        print(type(avatar))
        return redirect('/user/register/')
    return None


class TestView(View):
    def get(self, request):
        return HttpResponse('ttttttest')

    def post(self, request):
        return HttpResponse('post')
